package com.secsbrain.cmdb.web.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.secsbrain.cmdb.biz.utils.UserUtils;
import com.secsbrain.common.ResponseResult;
import com.secsbrain.sso.core.SsoHelper;
import com.secsbrain.cmdb.dal.model.AppEnv;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.secsbrain.cmdb.biz.IAppEnvService;
import com.secsbrain.cmdb.biz.impl.CommonService;

import javax.servlet.http.HttpServletRequest;

/**
 * 应用环境控制层
 * @author zhaoyi
 *
 */
@RestController
@RequestMapping("api/appEnv")
public class AppEnvController{

    private static final Logger logger = LoggerFactory.getLogger(AppEnvController.class);

    @Autowired
    IAppEnvService appEnvService;
    
    @Autowired
    CommonService commonService;
    
    /**
     * 功能描述: <br>
     * 查询列表
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponseResult queryList(Integer page, Integer limit, AppEnv params) {
    	Page<AppEnv> pageBean=new Page<>(page, limit);
    	Page<AppEnv> result=appEnvService.selectPage(pageBean, params);
    	Map<Long, String> idcMap=commonService.getIdcMap();
    	for(AppEnv appEnv:result.getRecords()){
    		appEnv.setIdcName(idcMap.get(appEnv.getIdcId()));
    	}
        return new ResponseResult<>(result);
    }
    
    /**
     * 功能描述: <br>
     * 查询所有列表
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/allList", method = RequestMethod.GET)
    public ResponseResult<?> queryAllList(AppEnv params) {
    	List<AppEnv> list=appEnvService.selectList(params);
    	return new ResponseResult<>(list);
    }
    
    /**
     * 功能描述: <br>
     * 新增
     *
     * @param appEnv
     * @return
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public ResponseResult<?> insert(HttpServletRequest httpServletRequest, @RequestBody AppEnv appEnv) {
        String envName=appEnv.getName();
        Assert.hasText(envName,"环境名不能为空");
        Assert.notNull(appEnv.getIdcId(),"机房不能为空");

        //租户下的环境标识是唯一的
        AppEnv appEnvParam=new AppEnv();
        appEnvParam.setName(envName);
        appEnvParam.setTenantCode(UserUtils.getTenantCode(httpServletRequest));
        int count = appEnvService.selectCount(appEnvParam);
        Assert.isTrue(count<=0,envName+"环境名已存在！");

        appEnv.setCreateTime(new Date());
    	appEnv.setCreator(SsoHelper.currentUser().getNickname());
    	appEnvService.insertSelective(appEnv);
    	return new ResponseResult<>();
    }
    
    /**
     * 功能描述: <br>
     * 更新
     *
     * @param appEnv
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public ResponseResult<?> update(HttpServletRequest httpServletRequest, @RequestBody AppEnv appEnv) {
        String envName=appEnv.getName();
        Assert.hasText(envName,"环境名不能为空");
        Assert.notNull(appEnv.getIdcId(),"机房不能为空");

        AppEnv appEnvOrg = appEnvService.selectById(appEnv.getId());
        Assert.notNull(appEnvOrg,"没有查询到环境");

        if(!appEnvOrg.getName().equals(envName)){
            //租户下的环境标识是唯一的
            AppEnv appEnvParam=new AppEnv();
            appEnvParam.setName(envName);
            appEnvParam.setTenantCode(UserUtils.getTenantCode(httpServletRequest));
            int count = appEnvService.selectCount(appEnvParam);
            Assert.isTrue(count<=0,envName+"环境名已存在！");
        }

        appEnv.setUpdateTime(new Date());
    	appEnv.setUpdator(SsoHelper.currentUser().getNickname());
    	appEnvService.updateSelectiveById(appEnv);
    	return new ResponseResult<>();
    }
    
    /**
     * 功能描述: <br>
     * 删除
     *
     * @param appEnv
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public ResponseResult<?> delete(@RequestBody AppEnv appEnv) {
        logger.info("删除环境：{}", JSON.toJSONString(appEnv));
    	appEnvService.deleteById(appEnv.getId());
    	return new ResponseResult<>();
    }
    
}
